// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
    pub val: i32,
    pub next: Option<Box<ListNode>>,
}

// impl ListNode {
//     #[inline]
//     fn new(val: i32) -> Self {
//         ListNode { next: None, val }
//     }
// }

struct Solution;

impl Solution {
    // 自己的解法  2021.2.21 周日 天气晴
    pub fn num_components(head: Option<Box<ListNode>>, g: Vec<i32>) -> i32 {
        let mut map = std::collections::HashSet::new();
        g.into_iter().for_each(|num| {
            map.insert(num);
        });
        let mut res = 0;
        let mut link_flag = false;
        let mut head = head.unwrap();
        loop {
            if map.contains(&head.val) {
                if !link_flag {
                    link_flag = true;
                    res += 1;
                }
            } else {
                link_flag = false;
            }
            if let Some(next) = head.next {
                head = next;
            } else {
                break;
            }
        }
        res
    }
}

fn main() {
    Solution::num_components(None, vec![]);
}
